<?php


namespace App\Services\Tools;

use Carbon\Carbon;
use Illuminate\Support\Arr;

class HelpService
{

    const APP_ENV_LOCAL = 'local';
    const APP_ENV_DEV = 'dev';
    const APP_ENV_TEST = 'test';
    const APP_ENV_PRE = 'pre';
    const APP_ENV_PRODUCTION = 'production';

    const APP_EXCEPT_ENVIRONMENT = [
        self::APP_ENV_LOCAL,
        self::APP_ENV_DEV,
        self::APP_ENV_TEST
    ];


    public static function perPage(): int
    {
        return request()->input('page_size') ?: 20;
    }

    public static function resultsPage($lengthAwarePaginator): array
    {
        return [
            'results' => $lengthAwarePaginator->items(),
            'total' => $lengthAwarePaginator->total(),
            'total_pages' => $lengthAwarePaginator->lastPage(),
            'page' => $lengthAwarePaginator->currentPage(),
            'page_size' => $lengthAwarePaginator->perPage(),
            'first' => $lengthAwarePaginator->currentPage() == 1,
            'last' => $lengthAwarePaginator->currentPage() == $lengthAwarePaginator->total()
        ];
    }

    public static function resultsPageSetData($lengthAwarePaginator, $data): array
    {
        return [
            'results' => $data,
            'total' => $lengthAwarePaginator->total(),
            'total_pages' => $lengthAwarePaginator->lastPage(),
            'page' => $lengthAwarePaginator->currentPage(),
            'page_size' => $lengthAwarePaginator->perPage(),
            'first' => $lengthAwarePaginator->currentPage() == 1,
            'last' => $lengthAwarePaginator->currentPage() == $lengthAwarePaginator->total()
        ];
    }

    public static function weekDay(Carbon $date): string
    {
        $weekDay = $date->dayOfWeek;

        switch ($weekDay) {
            case 0:
                $weekZh = '星期天';
                break;
            case 1:
                $weekZh = '星期一';
                break;
            case 2:
                $weekZh = '星期二';
                break;
            case 3:
                $weekZh = '星期三';
                break;
            case 4:
                $weekZh = '星期四';
                break;
            case 5:
                $weekZh = '星期五';
                break;
            case 6:
                $weekZh = '星期六';
                break;
            default:
                $weekZh = '星期' . $weekDay;
                break;
        }

        return $weekZh;
    }

    /**
     *      * 生成15位的订单号
     *      * @return string 订单号
     *      */
    public static function createOrderNum(): string
    {
        $year_code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        $date_code = array('0',
            '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
            'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y');
        //一共15位订单号,同一秒内重复概率1/10000000,26年一次的循环\
        //  随机数 2位
        return intval(date('Y')).date('m').date('d').$year_code[(intval(date('Y')) - 2010) % 26] . //年 1位
            strtoupper(dechex(date('m'))) . //月(16进制) 1位
            $date_code[intval(date('d'))] . //日 1位
            substr(time(), -5) . substr(microtime(), 2, 5) . //秒 5位 // 微秒 5位
            sprintf('%02d', rand(0, 99));
    }
    public static function createRandNum($prefix=''): string
    {
        $year_code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
        $date_code = array('0',
            '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
            'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y');
        //一共15位订单号,同一秒内重复概率1/10000000,26年一次的循环\
        //  随机数 2位
        return $prefix.$year_code[(intval(date('Y')) - 2010) % 26] . //年 1位
            strtoupper(dechex(date('m'))) . //月(16进制) 1位
            $date_code[intval(date('d'))] . //日 1位
            substr(time(), -3) . substr(microtime(), 2, 3) . //秒 3位 // 微秒 3位
            sprintf('%02d', rand(0, 99));
    }

    /*
     * 将秒转换为 分:秒
     * s int 秒数
     */
    public static function sToHs($s = 0): string
    {
        //计算分钟
        //算法：将秒数除以60，然后下舍入，既得到分钟数
        $h = floor($s / 60);
        //计算秒
        //算法：取得秒%60的余数，既得到秒数
        $s = $s % 60;
        //如果只有一位数，前面增加一个0
        $h = (strlen($h) == 1) ? '0' . $h : $h;
        $s = (strlen($s) == 1) ? '0' . $s : $s;
        return $h . ':' . $s;
    }

    /*
     * 将秒转换为 时:分:秒
     * s int 秒数
     */
    public static function sToHms($s = 0): string
    {
        if (!$s) {
            return '00:00:00';
        }
        $hours = floor($s / 3600);
        $minutes = floor(($s / 60) % 60);
        $seconds = $s % 60;
        return $hours . ':' . $minutes . ':' . $seconds;
    }

    /**
     * 秒转换成分钟
     */
    public static function sToi($s = 0): float
    {

        return round($s / 60);

    }

    /**
     * 获取环境变量
     */
    public static function getAppEnv()
    {
        $env = getenv('DEPLOY_APP_ENV');

        if (!$env) $env = env('APP_ENV');

        if (PHP_SAPI === 'cli') return $env ?: self::APP_ENV_PRODUCTION;

        if (in_array($env, self::APP_EXCEPT_ENVIRONMENT)) return $env;

        $http = request()->getHttpHost();

        $httpArray = explode('-', $http);

        $prefix = Arr::first($httpArray);

        switch ($prefix) {

            case self::APP_ENV_LOCAL:return self::APP_ENV_LOCAL;

            case self::APP_ENV_DEV: return self::APP_ENV_DEV;

            case 't': return self::APP_ENV_TEST;

            case self::APP_ENV_PRE: return self::APP_ENV_PRE;

            default: return self::APP_ENV_PRODUCTION;

        }
    }


}
